package com.fancy.middleware.netty.starter;

import com.fancy.middleware.netty.properties.NettyProperties;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class NettyStarter implements ApplicationRunner {
    @Autowired
    private ServerBootstrap serverBootstrap;

    @Autowired
    private NettyProperties nettyProperties;

    public void run(ApplicationArguments args) throws Exception {
        ChannelFuture bind =serverBootstrap
                .bind(nettyProperties.getPort());
        bind.sync().addListener((future)->{
            log.info("Listening port " + nettyProperties.getPort());
        });
    }
}
